package csci5115.autoalarm;

import java.util.Calendar;
import java.util.Locale;

import android.app.Activity;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class CalendarViewActivity extends Activity implements OnClickListener
{
	
	private TextView currentMonth;
	private ImageView prevMonth;
	private ImageView nextMonth;
	private GridView calendarView;
	private CalendarViewAdapter adapter;
	private Calendar _calendar;
	private int month, year;
	private static final String dateTemplate = "MMMM yyyy";


	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
		{
			super.onCreate(savedInstanceState);
			setContentView(R.layout.calendarview);

			_calendar = Calendar.getInstance(Locale.getDefault());
			month = _calendar.get(Calendar.MONTH);
			year = _calendar.get(Calendar.YEAR);

			

			prevMonth = (ImageView) this.findViewById(R.id.prevMonth);
			prevMonth.setOnClickListener(this);

			currentMonth = (TextView) this.findViewById(R.id.currentMonth);
			currentMonth.setText(DateFormat.format(dateTemplate, _calendar.getTime()));

			nextMonth = (ImageView) this.findViewById(R.id.nextMonth);
			nextMonth.setOnClickListener(this);

			calendarView = (GridView) this.findViewById(R.id.calendar);

			adapter = new CalendarViewAdapter(getApplicationContext(), R.id.day_gridcell, month, year);
			adapter.notifyDataSetChanged();
			calendarView.setAdapter(adapter);

		}

	public void onClick(View v)
		{
			if (v == prevMonth)
				{ 
					if (month <= 0)
						{
							month = 11;
							year--;
						} else
						{
							month--;
						}

					adapter = new CalendarViewAdapter(getApplicationContext(), R.id.day_gridcell, month, year);
					_calendar.set(year, month, _calendar.get(Calendar.DAY_OF_MONTH));
					currentMonth.setText(DateFormat.format(dateTemplate, _calendar.getTime()));

					adapter.notifyDataSetChanged();
					calendarView.setAdapter(adapter);
				}
			if (v == nextMonth)
				{
					if (month >= 11)
						{
							month = 0;
							year++;
						} else
						{
							month++;
						}

					adapter = new CalendarViewAdapter(getApplicationContext(), R.id.day_gridcell, month, year);
					_calendar.set(year, month, _calendar.get(Calendar.DAY_OF_MONTH));
					currentMonth.setText(DateFormat.format(dateTemplate, _calendar.getTime()));
					adapter.notifyDataSetChanged();
					calendarView.setAdapter(adapter);
				}
		}

	
}